/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.core.common
{
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	import mx.managers.ISystemManager;
	
	import org.etherframework.common.IDestroyable;
	import org.etherframework.core.utils.Dispatcher;
	
	[ExcludeClass]
	[Mixin]
	public class RootDispatcher extends Dispatcher implements IEventDispatcher, IDestroyable
	{
		private static var _systemManager:ISystemManager;
		private var _initialized:Boolean;
		private var _destroyed:Boolean;
		private var _topLevelApp:IEventDispatcher;
		
		public static function init(systemManager:ISystemManager):void
		{
			_systemManager = systemManager
		}
		
		public function initialize(topLevelApp:IEventDispatcher):void
		{
			if (_destroyed)
				return;
			if (_initialized)
				throw new IllegalOperationError("RootDispatcher is already initialized.");
			if (!topLevelApp)
				throw new ArgumentError("Argument topLevelApp:IEventDispatcher can not be null.");
			
			_initialized = true;
			_topLevelApp = topLevelApp;
			
			var dispatcher:IEventDispatcher = 
				(("initialized" in _topLevelApp) && topLevelApp["initialized"]) ? 
				_systemManager : _topLevelApp;
			
			if (dispatcher == _topLevelApp)
			{
				_topLevelApp.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			}
			
			switchSource(dispatcher);
		}
		
		private function addedToStageHandler(event:Event):void
		{
			_topLevelApp.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			switchSource(_systemManager);
		}
		
		// IEventDispatcher implementation
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, 
										 priority:int = 0, useWeakReference:Boolean = false):void
		{
			if (_destroyed)
				return;
			
			listen(this, type, listener, useCapture, priority, useWeakReference);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			unlisten(this, type, listener, useCapture);
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return false;
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return source.hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean
		{
			return source.willTrigger(type);
		}
		
		public function destroy():void
		{
			unlistenAll(this);
			_topLevelApp.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			_topLevelApp = null;
			_destroyed = true;
		}
		
		public function get initialized():Boolean { return _initialized; }
	}
}